package com.stemaini.core.doc;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.stemaini.ui.form.DialogPreprocessing;
import com.stemaini.utility.Koneksi;
import taini.koneksi.KoneksiAini;
import taini.query;

/**
 *
 * @author AdhiK-2008
 */
public class Terms {
    //private Koneksi theKoneksi;
    private KoneksiAini theKoneksi;
    private LinkedList listTerm;

    public Terms(Koneksi theKoneksi) {
        //this.theKoneksi = theKoneksi;
        this.listTerm = new LinkedList();
    }

    public Terms(KoneksiAini theKoneksi)
    {
        this.theKoneksi = theKoneksi;
        this.listTerm = new LinkedList();
    }
    
    public void append(String namaTerm)
    {
        TermsNode tn = new TermsNode(listTerm.size()+1,namaTerm);
        listTerm.add(tn);
    }
    
    public int getSize()
    {
        return this.listTerm.size();
    }
    
    public TermsNode getTermsNode(int i)
    {
        return (TermsNode)listTerm.get(i);
    }
    
    public String getTermsNodeWord(int i)
    {
        TermsNode tn = (TermsNode)listTerm.get(i);
        return tn.getNama_Term();
    }
    
    public int getTermsNodeID(int i)
    {
        TermsNode tn = (TermsNode)listTerm.get(i);
        return tn.getId_Term();
    }
    
    public int getTermsNodeID(String namaTerm)
    {
        for(int i=0;i<listTerm.size();i++)
        {
            TermsNode tn = (TermsNode)listTerm.get(i);
            if(tn.getNama_Term().equalsIgnoreCase(namaTerm))
            {
                return tn.getId_Term();
            }
        }
        return 0;
    }
    
//    public int getTermsNodeDfj(int indexTerm)
//    {
//        TermsNode tn = (TermsNode)listTerm.get(indexTerm);
//        return tn.getDfj();
//    }
//    
//    public int getTermsNodeDfj(String namaTerm)
//    {
//        for(int i=0;i<listTerm.size();i++)
//        {
//            TermsNode tn = (TermsNode)listTerm.get(i);
//            if(tn.getNama_Term().equalsIgnoreCase(namaTerm))
//            {
//                return tn.getDfj();
//            }
//        }
//        return 0;
//    }
//    
//    public void setTermsNodeDfj(String namaTerm, int dfj)
//    {
//        for(int i=0;i<listTerm.size();i++)
//        {
//            TermsNode tn = (TermsNode)listTerm.get(i);
//            if(tn.getNama_Term().equalsIgnoreCase(namaTerm))
//            {
//                tn.setDfj(dfj); return;
//            }
//        }
//    }
    
    public boolean isContain(String namaTerm)
    {
        for(int i=0;i<listTerm.size();i++)
        {
            TermsNode tn = (TermsNode)listTerm.get(i);
            if(tn.getNama_Term().equalsIgnoreCase(namaTerm))
            {
                return true;
            }
        }
        return false;
    }
    
    public void importFromDbase() throws SQLException
    {
        String s = "SELECT ID_TERM,NAMA_TERM FROM TERMS";
        ResultSet rs = theKoneksi.executeSelect(s);
        //ResultSet rs = new query().executeSQL(s);
        if(rs.first()){
            listTerm.clear();
            while(rs.isAfterLast()){
                TermsNode tn = new TermsNode(listTerm.size()+1,rs.getString("NAMA_TERM"));
                listTerm.add(tn);
                rs.next();
            }
        }
    }
    
    public void exportToDbase2(DialogPreprocessing dp)
    {
        try {
            int count = 0;
            String s = "INSERT INTO TERMS(ID_TERM,NAMA_TERM) VALUES(?,?)";

            PreparedStatement ps = theKoneksi.persiapkanStatement(s);
            for (int i = 0; i < listTerm.size(); i++) {
                TermsNode tm = (TermsNode) listTerm.get(i);
                ps.setInt(1, tm.getId_Term());
                ps.setString(2, tm.getNama_Term());
                ps.addBatch();
                count++;
                dp.tambahNilaiProgressBar(1);
                if (count == 150) {
                    ps.executeBatch();
                    count = 0;
                }
            }
            ps.executeBatch();
        } catch (SQLException ex) {
            Logger.getLogger(Terms.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Terms.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void exportToDbase(DialogPreprocessing dp)
    {
        try {
            int count = 0;
            String s = "INSERT INTO TERMS(ID_TERM,NAMA_TERM) VALUES ";
            for (int i = 0; i < listTerm.size(); i++) {
                TermsNode tm = (TermsNode) listTerm.get(i);
                s = s + "(" + tm.getId_Term() + ",'" + tm.getNama_Term() + "')";
                count++;
                if (count == 150) {
                    try {
                        s = s + ";";
                        theKoneksi.executeUpdate(s);
                        s = "INSERT INTO TERMS(ID_TERM,NAMA_TERM) VALUES ";
                        count = 0;
                    } catch (SQLException ex) {
                        Logger.getLogger(Terms.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IllegalStateException ex) {
                        Logger.getLogger(Terms.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    if(i!=listTerm.size()-1)
                        s = s + ",";
                }
                
                dp.tambahNilaiProgressBar(1);
            }
            if (count != 0) {
                if(s.endsWith(","))
                    s = s.substring(0,s.length()-1);
                s = s + ";";
                theKoneksi.executeUpdate(s);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Terms.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Terms.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    public void show()
    {
        for (int i = 0; i < listTerm.size(); i++) {
            TermsNode tm = (TermsNode)listTerm.get(i);
            System.out.println("ID:"+tm.getId_Term()+ " Kata:"+tm.getNama_Term());
        }
    }
    
}

